package com.booktrading.service.impl;

import com.booktrading.mapper.OrderMapper;
import com.booktrading.model.CategoryOrderStat;
import com.booktrading.model.Order;
import com.booktrading.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order createOrder(Order order) {
        // 生成订单号
        order.setOrderNo(generateOrderNo());
        order.setCreateTime(new Date());
        order.setStatus(0); // 0-待付款
        
        // 插入订单
        orderMapper.insert(order);
        return order;
    }

    @Override
    public Order getOrderById(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public List<Order> getBuyerOrders(Long buyerId) {
        return orderMapper.selectByBuyerId(buyerId);
    }

    @Override
    public List<Order> getSellerOrders(Long sellerId) {
        return orderMapper.selectBySellerId(sellerId);
    }

    @Override
    public boolean updateOrderStatus(Long id, Integer status) {
        return orderMapper.updateStatus(id, status) > 0;
    }

    @Override
    public boolean updateOrder(Order order) {
        return orderMapper.updateById(order) > 0;
    }

    @Override
    public Order getOrderByOrderNo(String orderNo) {
        return orderMapper.selectByOrderNo(orderNo);
    }

    @Override
    public List<Order> getAllOrders() {
        return orderMapper.selectAll();
    }

    @Override
    public int countAll() {
        return orderMapper.countAll();
    }

    @Override
    public Double sumAllAmount() {
        return orderMapper.sumAllAmount();
    }

    @Override
    public List<CategoryOrderStat> countByCategory() {
        return orderMapper.countByCategory();
    }

    // 生成订单号
    private String generateOrderNo() {
        return "ORDER" + System.currentTimeMillis();
    }
} 